<?php
namespace App\Http\Controllers\Business\App\Salesman;

use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Models\City;
use App\Models\SmsMessage;
use App\Models\User;
use App\Models\Recharge;
use App\Models\Shop;
use App\Models\ShopUser;
use App\Models\ShopSalesman;
use App\Models\SalesmanInfo;
use App\Models\ShopSalesmanDetail;
use App\Models\SalesmanSet;
use App\Models\SalesmanOrder;
use App\Models\SalesmanRecharge;
use App\Models\SalesmanRegister;
use App\Models\SalesmanWithdraw;
use App\Utils\DeviceTool;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Exception;
use Log;

class SalesmanManagement
{
    //商家地推, 增加等待注册的用户
    public static function addWaitUserByShop($mobile, $code, $shopId, $ip, $shopUserId='') {
        if(!$mobile||!$shopId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!SmsMessage::loginIdentifyingCodeVerify($mobile,$code,'4')) {
            return ['status'=>'failed','message'=>'验证码错误！'];
        }
        $shopUserMobile = '';
        $shopUserName = '';
        if($shopUserId != '') {
            $shopUser = ShopUser::where('id', $shopUserId)->select('mobile', 'name')->first();
            if($shopUser) {
                $shopUserMobile = $shopUser['mobile'];
                $shopUserName = $shopUser['name'];
            }
        }
        $data = [
            'type' => 'shopsalesman',
            'shopid' => $shopId,
            'shopuserid' => $shopUserId,
            'shopusermobile' => $shopUserMobile,
            'shopusername' => $shopUserName,
        ];
        Redis::setex('Shop:Salesman:WaitUser:' . $mobile, 84600, json_encode($data));
        return ['status'=>'success', 'message'=>'提交成功'];
    }
    //商家地推，localCity和cityName的区别是localcity是客户端传来的，cityname是通过ip取出来
    public static function addSalesmanByRegister($mobile, $deviceId, $cityName, $phonetype, $channel, $localCity) {
        if(!$mobile || !$deviceId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
//        $isThisDevice = User::where('deviceid', $deviceId)->first();
//        if($isThisDevice) {
//            return ['status'=>'failed', 'message'=>'该设备下已注册过'];
//        }
        if($channel == 'AppStore') {
            if(!strstr($phonetype,'iPhone') && !strstr($phonetype,'iPad') && !strstr($phonetype,'iPod')) {
                return ['status'=>'failed', 'message'=>'该设备下已注册过！'];
            }
        }
        $shopInfo = Redis::get('Shop:Salesman:WaitUser:'.$mobile);
        if(!$shopInfo) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $info = json_decode($shopInfo, true);
        Redis::del('Shop:Salesman:WaitUser:'.$mobile);
        if(!isset($info['type']) || !(($info['type'] == 'usersalesman')||($info['type'] == 'shopsalesman'))) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        if($info['type'] == 'shopsalesman') {
            //商家地推
            $shopId = $info['shopid'];
            $shopUserId = $info['shopuserid'];
            $shopUserMobile = $info['shopusermobile'];
            $shopUserName = $info['shopusername'];
            $result = self::addShopSalesmanByRegister($mobile, $shopId, $shopUserId, $shopUserMobile, $shopUserName,$cityName, $localCity, $deviceId);
        } elseif($info['type'] == 'usersalesman') {
            //用户地推
            $userId = $info['userid'];
            $result = self::addUserSalesmanByRegister($mobile, $userId, $cityName, $localCity, $deviceId);
        }
        if($result['status'] == 'failed') {
            return ['status'=>'failed', 'message'=>$result['message']];
        } else {
            return ['status' => 'success', 'data' => $result['data']];
        }
    }
    //商家版地推
    public static function addShopSalesmanByRegister($mobile, $shopId, $shopUserId, $shopUserMobile, $shopUserName,$cityName, $localCity, $deviceId='') {
        //获取商家地推信息
        $shopSalesman = ShopSalesman::where('shopid', $shopId)->where('status', '1')->first();
        if (!$shopSalesman) {
            return ['status' => 'failed', 'message' => '获取信息失败'];
        }
        $shopSalesmanType = $shopSalesman->type;
        $partnerRegisterNum = 0;
        //刷单限制条件
        //有效地推定义
        //地推结算
        if ($shopSalesmanType == 'common') {
            $getMoney = 5;
        } else {
            $partnerRegisterNum = $shopSalesman->partnerregisternum;
            if ($partnerRegisterNum < 1000) {
                $getMoney = 10;
            } else {
                $getMoney = 5;
            }
        }
        if($deviceId && (User::where('deviceid', $deviceId)->where('mobile', '<>', $mobile)->get()->count() > 0)) {
            $getMoney = 0;
        }
        $shopCityName = Shop::where('id', $shopId)->pluck('cityname')->first();
        // ip => 手机归属地 =>
        if(!$cityName || ( $shopCityName != $cityName)) {
            //ip获取的城市信息失败
            $mobileHomeLocationResult = DeviceTool::getMobileHomeLocationByK780($mobile);
            if($mobileHomeLocationResult['status'] == 'success') {
                $mobileHomeLocation = $mobileHomeLocationResult['data'];
                if($mobileHomeLocation != $shopCityName) {
                    //手机归属地不等于的话
                    if($shopCityName != $localCity) {
                        //定位不属于的话
                        $getMoney = 0;
                    }
                }
            }
        }
        if ($shopUserMobile == '') {
            $registerResource = '默认';
        } else {
            $registerResource = $shopUserName;
        }
        if (ShopSalesmanDetail::where('mobile', $mobile)->where('type', 'register')->get()->count() > 0) {
            return ['status' => 'failed', 'message' => '该手机已注册过'];
        }
        //商家地推详情
        $shopSalesmanDetail = ShopSalesmanDetail::create([
            'shopid' => $shopId,
            'shopusermobile' => $shopUserMobile,
            'shopuserid' => $shopUserId,
            'type' => 'register',
            'mobile' => $mobile,
            'getmoney' => $getMoney,
            'registersource' => $registerResource,
        ]);
        //商家地推
        if ($shopSalesmanType == 'common') {
            ShopSalesman::where('shopid', $shopId)->increment('commonregisternum', 1);
        } else {
            if ($partnerRegisterNum < 1000) {
                ShopSalesman::where('shopid', $shopId)->increment('partnerregisternum', 1);
            } else {
                ShopSalesman::where('shopid', $shopId)->increment('commonregisternum', 1);
            }
        }
        ShopSalesman::where('shopid', $shopId)->increment('balance', $getMoney);
        ShopSalesman::where('shopid', $shopId)->increment('getmoney', $getMoney);

        $data = [
            'salesmantype' => 'shopsalesman',
            'shopid' => $shopId,
            'cityname' => $cityName
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //用户地推注册
    public static function addUserSalesmanByRegister($mobile, $salesmanId, $cityName='', $localCity='', $deviceId='') {
        if(!$mobile||!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //刷单限制条件
        //TODO 规则还没有下来

        //有效地推定义
        $salesmanInfo = SalesmanInfo::where('salesmanid', $salesmanId)->first();
        if(!$salesmanInfo) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        if($salesmanInfo->status == 0) {
            return ['status'=>'failed', 'message'=>'地推帐号被禁用了'];
        }
        $salesmanSet = SalesmanSet::where('salesmantype', 'usersalesman')->where('cityid', $salesmanInfo->cityid)->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        //地推结算
        $validSalesmanRuleType = $salesmanSet->validsalesmanruletype;
        if($validSalesmanRuleType == 1) {
            //新用户通过地推专属二维码下载app，且在24小时内在app完成登录
            $isValid = 1;
        } else {
            //新用户登录app且消费xxx笔  ，单笔消费金额需达xxx以上 等
            $isValid = 0;
        }
        $accountType = $salesmanSet->accounttype;
        if(($accountType == 1) || ($accountType == 2) || ($accountType == 3) || ($accountType == 5) || ($accountType == 6)) {
            //类型为1表示每个有效地推的注册用户xxx元
            //类型为2表示每个有效地推的注册用户xxx元，且该用户的每笔消费实付金额满xxx以上的订单按xxx元／单计为地推订单收入
            //类型为3表示每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入
            //accounttype值为5表示每个有效地推的注册用户xx元,且该用户首笔金额满多少元以上的充值按xx元／单计为地推充值收入
            //accounttype值为6表示每个有效地推的注册用户xx元,且该用户前xx笔金额满多少元以上的充值按xx元／单计为地推充值收入
            
            $getMoney = $salesmanSet->registergetmoney;
        } elseif($accountType == 4) {
            //类型为4表示前xxx名有效地推用户：每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入；
            //之后的有效地推用户：每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入。
            //TODO topnum 是从0开始还是已注册量
            if($salesmanInfo->registernum <= $salesmanSet->topnum) {
                $getMoney = $salesmanSet->topregistergetmoney;
            } else {
                $getMoney = $salesmanSet->afterregistergetmoney;
            }
        } else {
            $getMoney = 0;
        }
        $salesmanCityName = City::where('id', $salesmanInfo->cityid)->pluck('name')->first();
        if((!$cityName || ( $salesmanCityName != $cityName)) && ($getMoney != 0) ) {
            //ip获取的城市信息失败
            $mobileHomeLocationResult = DeviceTool::getMobileHomeLocationByK780($mobile);
            if($mobileHomeLocationResult['status'] == 'success') {
                $mobileHomeLocation = $mobileHomeLocationResult['data'];
                if($mobileHomeLocation != $salesmanCityName) {
                    //手机归属地不等于的话
                    if($salesmanCityName != $localCity) {
                        //定位不属于的话
                        $getMoney = 0;
                        $isValid = 0;
                    }
                }
            }
        }
        if($deviceId && (User::where('deviceid', $deviceId)->where('mobile', '<>', $mobile)->get()->count() > 0)) {
            $isValid = 0;
            $getMoney = 0;
        }

        //新增salesman_register
        $salesmanRegister = SalesmanRegister::create([
            'salesmanid' => $salesmanId,
            'registermobile' => $mobile,
            'getmoney' => $getMoney,
            'isvalid' => $isValid,
            'status' => 0,
        ]);
        $salesmanRegister->save();
        //修改salesman_info表里的数据
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('registernum', 1);
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('currentregisternum', 1);
        if($getMoney != 0) {
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('registermoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('totalmoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('balance', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('balance', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('currenttotalmoney', $getMoney);
        }
        $data = [
            'salesmantype' => 'usersalesman',
            'salesmanid' => $salesmanId,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //版本v15的 注册时新增地推数据
    public static function addSalesmanByRegisterAndV15Version($mobile,$qrCodeUrl,$deviceId, $phoneType, $channel, $cityName, $localCity) {
        if(!$mobile||!$deviceId||!$phoneType||!$channel) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
//        $isThisDevice = User::where('deviceid', $deviceId)->where('mobile', '<>', $mobile)->first();
//        if($isThisDevice) {
//            return ['status'=>'failed', 'message'=>'该设备下已注册过'];
//        }
        if($channel == 'AppStore') {
            if(!strstr($phoneType,'iPhone') && !strstr($phoneType,'iPad') && !strstr($phoneType,'iPod')) {
                return ['status'=>'failed', 'message'=>'该设备下已注册过！'];
            }
        }
        if($qrCodeUrl) {
            //注册时输入手机号码弹出的二维码扫描出来或者首页扫描出来的
            $domainUrl = env('WEB_URL') . 'qrcode/';
            $url = str_replace($domainUrl, '', $qrCodeUrl);
            $arr = explode('/', $url);
            $salesmanType = isset($arr[0]) ? $arr[0] : '';
            $qrCodeInfo = isset($arr[1]) ? $arr[1] : '';
            if((($salesmanType!='shop') && ($salesmanType!='user')) || !$qrCodeInfo)  {
                return ['status'=>'failed', 'message'=>'数据解析错误'];
            }
            $shopUserId = strstr($qrCodeInfo, '?shopuserid=');
            if(!$shopUserId) {
                $qrCode = $qrCodeInfo;
            } else {
                $qrCode = substr($qrCodeInfo, 0, strrpos($qrCodeInfo, '?shopuserid='));
                $shopUserId = str_replace('?shopuserid=', '', $shopUserId);
                $shopUserId = trim($shopUserId);
            }
        } else {
            //用户用微信扫描商家二维码填写信息后的
            $shopInfo = Redis::get('Shop:Salesman:WaitUser:'.$mobile);
            if(!$shopInfo) {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
            $info = json_decode($shopInfo, true);
            Redis::del('Shop:Salesman:WaitUser:'.$mobile);
            if(!isset($info['type']) || !(($info['type'] == 'usersalesman')||($info['type'] == 'shopsalesman'))) {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
            if($info['type'] == 'shopsalesman') {
                //商家地推
                $qrCode = $info['shopid'];
                $shopUserId = $info['shopuserid'];
                $salesmanType = 'shop';
            } elseif($info['type'] == 'usersalesman') {
                //用户地推
                $qrCode = $info['userid'];
                $salesmanType = 'user';
                $shopUserId = '';
            }
        }

        if($salesmanType == 'shop') {
            //商家地推
            $result = self::addShopSalesmanByRegisterAndV15Version($mobile, $qrCode, $shopUserId, $cityName, $localCity, $deviceId);
        } else {
            //个人地推
            $result = self::addUserSalesmanByRegisterAndV15Version($mobile, $qrCode, $cityName, $localCity, $deviceId);
        }
        if($result['status'] == 'failed') {
            return ['status'=>'failed', 'message'=>$result['message']];
        } else {
            return ['status'=>'success', 'data'=>$result['data']];
        }
    }
    //版本v15的 注册时新增商家地推数据
    public static function addShopSalesmanByRegisterAndV15Version($mobile, $qrCode, $shopUserId, $cityName, $localCity, $deviceId='') {
        if(!$mobile||!$qrCode) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopId = Shop::where('id', $qrCode)->orwhere('qrcode', $qrCode)->pluck('id')->first();
        if(!$shopId) {
            return ['status' => 'failed', 'message' => '获取信息失败'];
        }
        //获取商家地推信息
        $shopSalesman = ShopSalesman::where('shopid', $shopId)->where('status', '1')->first();
        if (!$shopSalesman) {
            return ['status' => 'failed', 'message' => '获取信息失败'];
        }
        $shopSalesmanType = $shopSalesman->type;
        $partnerRegisterNum = 0;
        //刷单限制条件
        //有效地推定义
        //地推结算
        if ($shopSalesmanType == 'common') {
            $getMoney = 5;
        } else {
            $partnerRegisterNum = $shopSalesman->partnerregisternum;
            if ($partnerRegisterNum < 1000) {
                $getMoney = 10;
            } else {
                $getMoney = 5;
            }
        }
        $shopCityName = Shop::where('id', $shopId)->pluck('cityname')->first();
        // ip => 手机归属地 =>
        if(!$cityName || ( $shopCityName != $cityName)) {
            //ip获取的城市信息失败
            $mobileHomeLocationResult = DeviceTool::getMobileHomeLocationByK780($mobile);
            if($mobileHomeLocationResult['status'] == 'success') {
                $mobileHomeLocation = $mobileHomeLocationResult['data'];
                if($mobileHomeLocation != $shopCityName) {
                    //手机归属地不等于的话
                    if($shopCityName != $localCity) {
                        //定位不属于的话
                        $getMoney = 0;
                    }
                }
            }
        }
        if($deviceId && (User::where('deviceid', $deviceId)->where('mobile', '<>', $mobile)->get()->count() > 0)) {
            $getMoney = 0;
        }
        $shopUserMobile = '';
        if ($shopUserId == '') {
            $registerResource = '默认';
        } else {
            $shopUser = ShopUser::where('id', $shopUserId)->select('mobile', 'name')->first();
            if($shopUser) {
                $registerResource = $shopUser['name'];
                $shopUserMobile = $shopUser['mobile'];
            }
        }
        if (ShopSalesmanDetail::where('mobile', $mobile)->where('type', 'register')->get()->count() > 0) {
            return ['status' => 'failed', 'message' => '该手机已注册过'];
        }
        //商家地推详情
        $shopSalesmanDetail = ShopSalesmanDetail::create([
            'shopid' => $shopId,
            'shopusermobile' => $shopUserMobile,
            'shopuserid' => $shopUserId,
            'type' => 'register',
            'mobile' => $mobile,
            'getmoney' => $getMoney,
            'registersource' => $registerResource,
        ]);
        //商家地推
        if ($shopSalesmanType == 'common') {
            ShopSalesman::where('shopid', $shopId)->increment('commonregisternum', 1);
        } else {
            if ($partnerRegisterNum < 1000) {
                ShopSalesman::where('shopid', $shopId)->increment('partnerregisternum', 1);
            } else {
                ShopSalesman::where('shopid', $shopId)->increment('commonregisternum', 1);
            }
        }
        ShopSalesman::where('shopid', $shopId)->increment('balance', $getMoney);
        ShopSalesman::where('shopid', $shopId)->increment('getmoney', $getMoney);

        $data = [
            'salesmantype' => 'shopsalesman',
            'shopid' => $shopId,
            'cityname' => $cityName
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //版本v15的 注册时新增个人地推数据
    public static function addUserSalesmanByRegisterAndV15Version($mobile, $qrCode,$cityName, $localCity, $deviceId='') {
        if(!$mobile||!$qrCode) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //刷单限制条件
        //TODO 规则还没有下来
        //有效地推定义
        $salesmanInfo = SalesmanInfo::where('salesmanid', $qrCode)->orwhere('qrcode', $qrCode)->first();
        if(!$salesmanInfo) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $salesmanId = $salesmanInfo->salesmanid;
        if($salesmanInfo->status == 0) {
            return ['status'=>'failed', 'message'=>'地推帐号被禁用了'];
        }
        $salesmanSet = SalesmanSet::where('salesmantype', 'usersalesman')->where('id', $salesmanInfo->salesmansetid)
            ->where('cityid', $salesmanInfo->cityid)->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        //地推结算
        $validSalesmanRuleType = $salesmanSet->validsalesmanruletype;
        if(($validSalesmanRuleType == 1)) {
            //新用户通过地推专属二维码下载app，且在24小时内在app完成登录
            $isValid = 1;
        } else {
            //新用户登录app且消费xxx笔  ，单笔消费金额需达xxx以上 等
            $isValid = 0;
        }
        $accountType = $salesmanSet->accounttype;
        if(($accountType == 1) || ($accountType == 2) || ($accountType == 3) || ($accountType == 5) || ($accountType == 6)) {
            //类型为1表示每个有效地推的注册用户xxx元
            //类型为2表示每个有效地推的注册用户xxx元，且该用户的每笔消费实付金额满xxx以上的订单按xxx元／单计为地推订单收入
            //类型为3表示每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入
            //accounttype值为5表示每个有效地推的注册用户xx元,且该用户首笔金额满多少元以上的充值按xx元／单计为地推充值收入
            //accounttype值为6表示每个有效地推的注册用户xx元,且该用户前xx笔金额满多少元以上的充值按xx元／单计为地推充值收入
            $getMoney = $salesmanSet->registergetmoney;
        } elseif($accountType == 4) {
            //类型为4表示前xxx名有效地推用户：每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入；
            //之后的有效地推用户：每个有效地推的注册用户xxx元，且该用户的每笔消费按实付金额的xxx%计为地推订单收入。
            //TODO topnum 是从0开始还是已注册量
            if($salesmanInfo->registernum < $salesmanSet->topnum) {
                $getMoney = $salesmanSet->topregistergetmoney;
            } else {
                $getMoney = $salesmanSet->afterregistergetmoney;
            }
        } else {
            $getMoney = 0;
        }
        $salesmanCityName = City::where('id', $salesmanInfo->cityid)->pluck('name')->first();
        if((!$cityName || ( $salesmanCityName != $cityName)) && ($getMoney != 0) ) {
            //ip获取的城市信息失败
            $mobileHomeLocationResult = DeviceTool::getMobileHomeLocationByK780($mobile);
            if($mobileHomeLocationResult['status'] == 'success') {
                $mobileHomeLocation = $mobileHomeLocationResult['data'];
                if($mobileHomeLocation != $salesmanCityName) {
                    //手机归属地不等于的话
                    if($salesmanCityName != $localCity) {
                        //定位不属于的话
                        $getMoney = 0;
                        $isValid = 0;
                    }
                }
            }
        }

        if($deviceId && (User::where('deviceid', $deviceId)->where('mobile', '<>', $mobile)->get()->count() > 0)) {
            $isValid = 0;
            $getMoney = 0;
        }
        //新增salesman_register
        $salesmanRegister = SalesmanRegister::create([
            'salesmanid' => $salesmanId,
            'registermobile' => $mobile,
            'getmoney' => $getMoney,
            'isvalid' => $isValid,
            'status' => 0,
        ]);
        $salesmanRegister->save();
        //修改salesman_info表里的数据
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('registernum', 1);
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('currentregisternum', 1);
        if($getMoney != 0) {
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('registermoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('totalmoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('balance', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('currenttotalmoney', $getMoney);
        }
        $data = [
            'salesmantype' => 'usersalesman',
            'salesmanid' => $salesmanId,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //新增商家地推订单的
    public static function addShopSalesmanByOrder($userId, $orderNumber, $orderAmount, $orderTime) {
        if(!$userId||!$orderNumber||!$orderAmount||!$orderTime||($orderAmount<50)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $user = User::where('id', $userId)->select('mobile', 'shopid')->first();
        if(!$user) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $shopId = $user->shopid;
        $mobile = $user->mobile;
        if(($shopId == 0) || ($shopId=='')) {
            return ['status'=>'failed','message'=>'不需要添加地推订单'];
        }
        $shopSalesman = ShopSalesman::where('shopid', $shopId)->where('status', 1)
            ->select( 'contractstarttime', 'contractlasttime', 'type')->first();
        if(!$shopSalesman) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $contractStartTime = $shopSalesman->contractstarttime;
        $contractLastTime = $shopSalesman->contractlasttime;
        $shopSalesmanType = $shopSalesman->type;
        if((strtotime($orderTime) < strtotime($contractStartTime . " 00:00:00")) || (strtotime($orderTime) > strtotime($contractLastTime . " 23:59:59"))) {
            return ['status'=>'failed', 'message'=>'合约到期了'];
        }
        if($shopSalesmanType == 'partner') {
            $getMoney = 2;
        } elseif($shopSalesmanType == 'common') {
            $getMoney = 1;
        } else {
            return ['status'=>'failed', 'message'=>'类型不对'];
        }

        //商家地推详情
        $shopSalesmanDetail = ShopSalesmanDetail::create([
            'shopid' => $shopId,
            'shopusermobile' => '',
            'type' => 'order',
            'mobile' => $mobile,
            'getmoney' => 1,
            'ordernumber' => $orderNumber,
            'orderamount' => $orderAmount,
            'ordertime' => date('Y-m-d H:i:s', strtotime($orderTime))
        ]);
        ShopSalesman::where('shopid', $shopId)->increment('balance', 1);
        return ['status'=>'success', 'message'=>'新增成功'];
    }
    //新增个人地推订单
    public static function addUserSalesmanByOrder($userId, $orderId) {

    }
    //新增个人地推充值
    public static function addUserSalesmanByRecharge($userId, $rechargeNumber, $rechargeAmount) {
        if(!$userId||!$rechargeNumber||!$rechargeAmount) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //判断是否已经执行过
        if(SalesmanRecharge::where('registerid', $userId)->where('rechargenumber', $rechargeNumber)->get()->count() > 0) {
            return ['status'=>'failed', 'message'=>'已经写过一次数据'];
        }
        $user = User::where('id', $userId)->select('salesmanid', 'mobile')->first();
        if(!$user) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $salesmanId = $user->salesmanid;
        $mobile = $user->mobile;
        $salesmanInfo = SalesmanInfo::where('salesmanid', $salesmanId)->where('status', 1)->first();
        if(!$salesmanInfo) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        if($salesmanInfo->effectivedate < date('Y-m-d H:i:s')) {
            $salesmanSetId = $salesmanInfo->salesmansetid;
        } else {
            $salesmanSetId = $salesmanInfo->lastsalesmanid;
        }
        $salesmanSet = SalesmanSet::where('id', $salesmanSetId)->where('salesmantype', 'usersalesman')->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $accountType = $salesmanSet->accounttype;
        $fiveMinuteAgo = date('Y-m-d H:i:s', time() - 5*60);
        if($accountType == 5) {
            //判断是否有过充值记录
            $rechargeCount = Recharge::where('userid', $userId)->where('created_at', '<', $fiveMinuteAgo)->get()->count();
            if($rechargeCount > 0) {
                return ['status'=>'failed', 'message'=>'已经充值过一次'];
            }
            if($rechargeAmount < $salesmanSet->rechargeminamount) {
                return ['status'=>'failed', 'message'=>'充值金额小于最小金额'];
            }
            $getMoney = $salesmanSet->rechargegetmoney;
        } elseif ($accountType == 6) {
            $rechargeCount = Recharge::where('userid', $userId)->where('created_at', '<', $fiveMinuteAgo)->get()->count();
            if($salesmanSet->rechargetopnum < $rechargeCount) {
                return ['status'=>'failed', 'message'=>'笔数已经满了'];
            }
            if($rechargeAmount < $salesmanSet->rechargeminamount) {
                return ['status'=>'failed', 'message'=>'充值金额小于最小金额'];
            }
            $getMoney = $salesmanSet->rechargegetmoney;
        } else {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        if($getMoney > 0) {
            $isValid = 1;
        } else {
            $isValid = 0;
        }
        $salesmanRecharge = SalesmanRecharge::create([
            'salesmanid' => $salesmanId,
            'registermobile' => $mobile,
            'registerid' => $userId,
            'rechargenumber' => $rechargeNumber,
            'rechargeamount' => $rechargeAmount,
            'getmoney' => $getMoney,
            'isvalid' => $isValid,
            'status' => 0,
        ]);
        $salesmanRecharge->save();
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('rechargenum', 1);
        if($getMoney != 0) {
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('rechargevalidnum', 1);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('rechargemoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('totalmoney', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('balance', $getMoney);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('currentrechargenum', 1);
            SalesmanInfo::where('salesmanid', $salesmanId)->increment('currenttotalmoney', $getMoney);
        }
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('rechargetotalamount', $rechargeAmount);
        return ['status'=>'success', 'message'=>'新增数据成功'];
    }
    //通过ip和shopid判断是不是同城
    public static function isTheSameCity($shopId, $ip) {
        if(!$shopId || !$ip) {
            return ['status'=>'failed', 'message'=>'参数错误！'];
        }
        $result = DeviceTool::getCityByIp($ip);
        if($result['status'] == 'failed') {
            return ['status'=>'failed', 'message'=>'获取失败'];
        }
        $data = $result['data'];
        $city = $data['city'];
        if(Shop::where('id', $shopId)->pluck('cityname')->first() != $city) {
            return ['status'=>'failed', 'message'=>'不是同一个城市'];
        } else {
            return ['status'=>'success', 'data'=>['cityname'=>$city]];
        }
    }
    //个人地推，增加等待注册用户
    public static function addWaitUserByUser($mobile, $code, $userId) {
        if(!$mobile||!$userId||!$code) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!SmsMessage::loginIdentifyingCodeVerify($mobile,$code,'4')) {
            return ['status'=>'failed','message'=>'验证码错误！'];
        }
        //地推信息
        $data = [
            'type' => 'usersalesman',
            'userid' => $userId,
        ];
        Redis::setex('Shop:Salesman:WaitUser:' . $mobile, 84600, json_encode($data));
        return ['status'=>'success', 'message'=>'提交成功'];
    }
    //用户端个人地推的首页
    public static function userSalesmanIndex($userId, $cityId) {
        if(!$userId||!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $salesmanSetId = SalesmanInfo::where('salesmanid', $userId)->pluck('salesmansetid')->first();
        $salesmanSet = SalesmanSet::where('id', $salesmanSetId)->where('salesmantype', 'usersalesman')->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $validSalesmanRuleType = $salesmanSet->validsalesmanruletype;
        if($validSalesmanRuleType == 1) {
            $showRule = "邀请同城新用户，以下步骤2选1完成操作即可。\n1.新用户扫码→填写手机号和验证码→下载App→2小时内完成登录。\n2.新用户下载App→首次登录的“选填获取邀请码”扫地推码→完成登录。首次登录时未扫码，可在2小时内使用App的“扫一扫”扫码。\n";
        } elseif($validSalesmanRuleType == 2) {
            $showRule = "新用户登录app且消费" . $salesmanSet->validorderminnum . "笔，单笔消费金额需达".$salesmanSet->validorderminamount."以上。";
        } else {
            $showRule = '';
        }
        $showRule .= "注：非同城用户或同一设备登录的新用户会被判定为无效。";
        $accountType = $salesmanSet->accounttype;
        if(($accountType == 2) || ($accountType == 3) || ($accountType == 4)) {
            $isShowSalesmanOrder = 1;
        } else {
            $isShowSalesmanOrder = 0;
        }
        if(($accountType == 5) || ($accountType == 6)) {
            $isShowSalesmanRecharge = 1;
        } else {
            $isShowSalesmanRecharge = 0;
        }
        $data = [
            'salesmanrules' => $showRule,
            'isshowsalesmanorder' => $isShowSalesmanOrder,
            'isshowsalesmanrecharge' => $isShowSalesmanRecharge,
        ];
        return ["status"=>'success', 'data'=>$data];
    }
    //个人地推注册用户
    public static function userSalesmanRegisterList($salesmanId, $startTime, $lastTime, $page=1, $length=20) {
        if(!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2017-03-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page-1) * $length;
        $count = SalesmanRegister::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)->count();
        $validCount = SalesmanRegister::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)->where('isvalid', 1)->count();
        $lists = SalesmanRegister::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)
            ->select('created_at', 'registermobile', 'isvalid')
            ->skip($skip)->take($length)->get();
        foreach($lists as $k=>$v) {
            $lists[$k]['registermobile'] = str_replace(substr($lists[$k]['registermobile'], 3, 4), '****', $lists[$k]['registermobile']);
        }
        $data = [
            'count' => $count,
            'validcount' => $validCount,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //个人地推订单列表
    public static function userSalesmanOrderList($salesmanId, $startTime, $lastTime, $page=1, $length=20) {
        if(!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2017-03-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page-1) * $length;
        $count = SalesmanOrder::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)->count();
        $lists = SalesmanOrder::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)
            ->select('created_at', 'registermobile', 'orderamount')
            ->skip($skip)->take($length)->get();
        foreach($lists as $k=>$v) {
            $lists[$k]['registermobile'] = str_replace(substr($lists[$k]['registermobile'], 3, 4), '****', $lists[$k]['registermobile']);
        }
        $data = [
            'count' => $count,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //个人地推充值列表
    public static function userSalesmanRechargeList($salesmanId, $startTime, $lastTime, $page=1, $length=20) {
        if(!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2017-03-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page-1) * $length;
        $count = SalesmanRecharge::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)->count();
        $lists = SalesmanRecharge::where('salesmanid', $salesmanId)->where('created_at', '>=', $startTime)
            ->where('created_at', '<=', $lastTime)
            ->select('created_at', 'registermobile', 'rechargeamount')
            ->skip($skip)->take($length)->get();
        foreach($lists as $k=>$v) {
            $lists[$k]['registermobile'] = str_replace(substr($lists[$k]['registermobile'], 3, 4), '****', $lists[$k]['registermobile']);
        }
        $data = [
            'count' => $count,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推结款
    public static function  userSalesmanWithdrawal($salesmanId, $cityId) {
        if(!$salesmanId||!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $salesmanSetId = SalesmanInfo::where('salesmanid', $salesmanId)->pluck('salesmansetid')->first();
        $salesmanSet = SalesmanSet::where('id', $salesmanSetId)->where('salesmantype', 'usersalesman')->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //结算方式
        $accountPeriod = $salesmanSet->accountperiod;
        if($accountPeriod == 0) {
            $shopAccountPeriod = '';
        } elseif($accountPeriod == 1) {
            $shopAccountPeriod = "每".$accountPeriod."月,";
        } else {
            $shopAccountPeriod = "每".$accountPeriod."个月,";
        }
        $accountType = $salesmanSet->accounttype;
        if($accountType == 1) {
            $withdrawalMethod = $shopAccountPeriod."每个有效地推的注册用户".$salesmanSet->registergetmoney."元。";
        } elseif($accountType == 2) {
            $withdrawalMethod = $shopAccountPeriod."每个有效地推的注册用户".$salesmanSet->registergetmoney."元，且该用户的每笔消费实付金额满".$salesmanSet->accountorderminamount."以上的订单按".$salesmanSet->ordergetmoney."元／单计为地推订单收入。";
        } elseif($accountType == 3) {
            $withdrawalMethod = $shopAccountPeriod."每个有效地推的注册用户".$salesmanSet->registergetmoney."元，且该用户的每笔消费按实付金额的".$salesmanSet->ordergetratio."%计为地推订单收入。";
        } elseif($accountType == 4) {
            $withdrawalMethod = $shopAccountPeriod."前".$salesmanSet->topnum."名有效地推用户：每个有效地推的注册用户".$salesmanSet->topregistergetmoney
                ."元，且该用户的每笔消费按实付金额的".$salesmanSet->topordergetratio."%计为地推订单收入，之后的有效地推用户：每个有效地推的注册用户".$salesmanSet->afterregistergetmoney."元，且该用户的每笔消费按实付金额的".$salesmanSet->afterordergetratio."%计为地推订单收入。";
        } elseif($accountType == 5) {
            $withdrawalMethod = $shopAccountPeriod."每个有效地推的注册用户".$salesmanSet->registergetmoney."元,且该用户首笔金额满". $salesmanSet->rechargeminamount."元以上的充值按".$salesmanSet->rechargegetmoney."元／单计为地推充值收入";
        } elseif($accountType == 6) {
            $withdrawalMethod = $shopAccountPeriod."每个有效地推的注册用户".$salesmanSet->registergetmoney."元,且该用户前".$salesmanSet->rechargetopnum."笔金额满". $salesmanSet->rechargeminamount."元以上的充值按".$salesmanSet->rechargegetmoney."元／单计为地推充值收入";
        } else{
            $withdrawalMethod = "";
        }
        //余额
        $balance = SalesmanInfo::where('salesmanid', $salesmanId)->pluck('balance')->first();
        //总数
        $count = SalesmanWithdraw::where('salesmanid', $salesmanId)->where('status', 1)->count();
        //总计
        $sum = SalesmanWithdraw::where('salesmanid', $salesmanId)->where('status', 1)->sum('accountmoney');
        if($sum == '') {
            $sum = 0;
        }
        //列表
        $lists = SalesmanWithdraw::where('salesmanid', $salesmanId)->where('status', 1)->orderBy('created_at', 'desc')
            ->select('accounttime', 'accountmoney')->get();
        $data = [
            'withdrawalmethod' => $withdrawalMethod,
            'balance' => $balance,
            'count' => $count,
            'sum' => $sum,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推余额
    public static function userSalesmanWithdrawalListByDay($salesmanId, $myDate) {
        if(!$salesmanId||!$myDate) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //上一次提现日期
        $lastWithdrawalTime = SalesmanWithdraw::where('salesmanid', $salesmanId)->where('accounttime', '<', $myDate)
            ->orderBy('accounttime', 'desc')->pluck('accounttime')->first();
        if($lastWithdrawalTime) {
            $dayTime = strtotime($myDate) - strtotime($lastWithdrawalTime);
            $days = round($dayTime/60/60/24);
        } else {
            $days = 30;
        }
        $lists = [];
        if($days > 0) {
            //过滤掉当天的提现记录
            for ($i = $days - 1; $i >= 0; $i--) {
                $day = date('Y-m-d', strtotime($myDate) - 60 * 60 * 24 * $i);
                $dayStartTime = $day . " 00:00:00";
                $dayEndTime = $day . " 23:59:59";
                $registerNum = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->count();
                $validOrderNum = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->count();
                $sumOrderAmount = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->sum('orderamount');
                $sumOrderAmount = ($sumOrderAmount != '') ? $sumOrderAmount : 0.00;
                if (($registerNum != 0) || ($validOrderNum != 0)) {
                    $list = [
                        'date' => $day,
                        'registernum' => $registerNum,
                        'ordernum' => $validOrderNum,
                        'orderamount' => $sumOrderAmount
                    ];
                    array_push($lists, $list);
                }
            }
        }
        return ['status'=>'success', 'data'=>$lists];
    }
    //版本15的地推余额
    public static function userSalesmanWithdrawalListByDayAndV15Version($salesmanId, $myDate) {
        if(!$salesmanId||!$myDate) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //上一次提现日期
        $lastWithdrawalTime = SalesmanWithdraw::where('salesmanid', $salesmanId)->where('accounttime', '<', $myDate)
            ->orderBy('accounttime', 'desc')->pluck('accounttime')->first();
        if($lastWithdrawalTime) {
            $dayTime = strtotime($myDate) - strtotime($lastWithdrawalTime);
            $days = round($dayTime/60/60/24);
        } else {
            $days = 30;
        }
        $lists = [];
        $salesmanSetId = SalesmanInfo::where('salesmanid', $salesmanId)->pluck('salesmansetid')->first();
        $salesmanSet = SalesmanSet::where('id', $salesmanSetId)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $accountType = $salesmanSet->accounttype;
        if($accountType == 1) {
            $thead = 1; //表示表头：时间  注册数
        } elseif(($accountType == 2) || ($accountType == 3) || ($accountType == 4)) {
            $thead = 3; //表示表头：时间  注册数 地推订单数  订单实付
        } elseif(($accountType == 5) || ($accountType == 6)) {
            $thead = 2; //表示表头：时间  注册数 地推充值数
        } else {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        if($days > 0) {
            //过滤掉当天的提现记录
            for ($i = $days - 1; $i >= 0; $i--) {
                $day = date('Y-m-d', strtotime($myDate) - 60 * 60 * 24 * $i);
                $dayStartTime = $day . " 00:00:00";
                $dayEndTime = $day . " 23:59:59";
                $registerNum = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->count();
                $validOrderNum = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->count();
                $sumOrderAmount = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->sum('orderamount');
                $sumOrderAmount = ($sumOrderAmount != '') ? $sumOrderAmount : 0.00;
                $rechargeNum = SalesmanRecharge::where('salesmanid', $salesmanId)->where('isvalid', 1)
                    ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                    ->count();
                if (($registerNum != 0) || ($validOrderNum != 0) || ($rechargeNum != 0)) {
                    $list = [
                        'date' => $day,
                        'registernum' => $registerNum,
                        'ordernum' => $validOrderNum,
                        'orderamount' => $sumOrderAmount,
                        'rechargenum' => $rechargeNum,
                    ];
                    array_push($lists, $list);
                }
            }
        }
        $data = [
            'lists' => $lists,
            'thead' => $thead
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推余额
    public static function userSalesmanBalance($salesmanId, $page=1, $perpage=20) {
        if(!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $lists = [];
        $first = ($page - 1) * $perpage;
        $last = $page * $perpage;
        for($i=$first; $i<$last; $i++) {
            $day = date('Y-m-d', strtotime('-' . $i . "days"));
            $dayStartTime = $day. " 00:00:00";
            $dayEndTime = $day. " 23:59:59";
            $registerNum = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->count();
            $validOrderNum = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->count();
            $sumOrderAmount = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->sum('orderamount');
            if(($registerNum != 0) || ($validOrderNum != 0)) {
                $list = [
                    'date' => $day,
                    'registernum' => $registerNum,
                    'ordernum' => $validOrderNum,
                    'orderamount' => $sumOrderAmount
                ];
                array_push($lists, $list);
            }
        }
        $data = [
            'lists' => $lists,
            'count' => count($lists),
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //版本15地推余额
    public static function userSalesmanBalanceByV15Version($salesmanId, $page=1, $perpage=20) {
        if(!$salesmanId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $lists = [];
        $first = ($page - 1) * $perpage;
        $last = $page * $perpage;
        $salesmanSetId = SalesmanInfo::where('salesmanid', $salesmanId)->pluck('salesmansetid')->first();
        $salesmanSet = SalesmanSet::where('id', $salesmanSetId)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $accountType = $salesmanSet->accounttype;
        if($accountType == 1) {
            $thead = 1; //表示表头：时间  注册数
        } elseif(($accountType == 2) || ($accountType == 3) || ($accountType == 4)) {
            $thead = 3; //表示表头：时间  注册数 地推订单数  订单实付
        } elseif(($accountType == 5) || ($accountType == 6)) {
            $thead = 2; //表示表头：时间  注册数 地推充值数
        } else {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        for($i=$first; $i<$last; $i++) {
            $day = date('Y-m-d', strtotime('-' . $i . "days"));
            $dayStartTime = $day. " 00:00:00";
            $dayEndTime = $day. " 23:59:59";
            $registerNum = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->count();
            $validOrderNum = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->count();
            $sumOrderAmount = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->sum('orderamount');
            $rechargeNum = SalesmanRecharge::where('salesmanid', $salesmanId)->where('isvalid', 1)
                ->where('created_at', '>=', $dayStartTime)->where('created_at', '<=', $dayEndTime)
                ->count();
            if(($registerNum != 0) || ($validOrderNum != 0)) {
                $list = [
                    'date' => $day,
                    'registernum' => $registerNum,
                    'ordernum' => $validOrderNum,
                    'orderamount' => $sumOrderAmount,
                    'rechargenum' => $rechargeNum
                ];
                array_push($lists, $list);
            }
        }
        $data = [
            'thead' => $thead,
            'lists' => $lists,
            'count' => count($lists),
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}